#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int m, n;
    int dx[4] = { 0, 0, 1, -1 };
    int dy[4] = { 1, -1, 0, 0 };

    void solve(vector<vector<char>>& board) {
        m = board.size(), n = board[0].size();
        for (int i = 0; i < n; i++) {
            if (board[0][i] == 'O') {
                board[0][i] = 'A';
                dfs(board, 0, i, false);
            }
            if (board[m - 1][i] == 'O') {
                board[m - 1][i] = 'A';
                dfs(board, m - 1, i, false);
            }
        }
        for (int i = 0; i < m; i++) {
            if (board[i][0] == 'O') {
                board[i][0] = 'A';
                dfs(board, i, 0, false);
            }
            if (board[i][n - 1] == 'O') {
                board[i][n - 1] = 'A';
                dfs(board, i, m - 1, false);
            }
        }
        for (int i = 1; i < m - 1; i++) {
            for (int j = 1; j < n - 1; j++) {
                if (board[i][j] == 'O') {
                    board[i][j] = 'X';
                    dfs(board, i, j, true);
                }
            }
        }
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (board[i][j] == 'A') board[i][j] = 'O';
            }
        }
    }
    void dfs(vector<vector<char>>& board, int i, int j, bool f) {
        if (f) {
            for (int k = 0; k < 4; k++) {
                int x = i + dx[k], y = j + dy[k];
                if (x >= 1 && x < m - 1 && y >= 1 && y < n - 1 &&
                    board[x][y] == 'O') {
                    board[x][y] = 'X';
                    dfs(board, x, y, true);
                }
            }
        }
        else {
            for (int k = 0; k < 4; k++) {
                int x = i + dx[k], y = j + dy[k];
                if (x >= 1 && x < m - 1 && y >= 1 && y < n - 1 &&
                    board[x][y] == 'O') {
                    board[x][y] = 'A';
                    dfs(board, x, y, false);
                }
            }
        }
    }
};